package com.itheima.config;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;

/**
 * ClassName:MybatisConfig
 * Package:com.itheima.config
 * Description:
 *
 * @Author 王久顺
 * @Create 2024/12/3 19:16
 * @Version 1.0
 */
public class MybatisConfig {
    /**
     * 定义bean，SqlSessionFactoryBean，用于产生SqlSessionFactory对象
     * @param dataSource
     * @return
     */
    @Bean
    public SqlSessionFactoryBean getSqlSessionFactoryBean(DataSource dataSource){
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        //设置模型类的别名扫描
        sqlSessionFactoryBean.setTypeAliasesPackage("com.itheima.pojo");
        //设置数据源
        sqlSessionFactoryBean.setDataSource(dataSource);
        return sqlSessionFactoryBean;
    }

    /**
     * <!--映射文件扫描包路径-->
     *     <mappers>
     *         <package name="com.itheima.dao"></package>
     *     </mappers>
     * @return
     */
    @Bean
    public MapperScannerConfigurer getMapperScannerConfigurer(){
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setBasePackage("com.itheima.dao");
        return mapperScannerConfigurer;
    }
}
